<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\BillOrder;
use app\common\model\LogType;
use app\common\model\WalletToken;
use fast\Tree;
use think\Db;

/**
 * 交易管理
 *
 * @icon   fa fa-list
 */
class Trade extends Backend
{
    protected $TradeOrderStatus = [0 => '挂卖中', 1 => '交易已成功'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('TradeOrder');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model->alias('bo')
                ->join('goods_order go', 'go.id=bo.tradeid', 'left')
                ->where(['bo.type' => LogType::get_type_id('sell_pets')])
                ->where($where)
                ->field('bo.*,go.id as go_id,go.name')
                ->order($sort, $order)
                ->paginate($limit);
            $rows = $list->items();
            foreach ($rows as $row) {
                foreach ($this->TradeOrderStatus as $key => $orderStatus) {
                    if ($key == $row['status']) {
                        $row->status_str = $orderStatus;
                    }
                }
                $row->seller_username = model('user')->where(['id' => $row['uid']])->value('username');
                $row->buyer_username = model('user')->where(['id' => $row['to_uid']])->value('username');
            }

            $result = array($list->total(), "rows" => $rows);

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * Selectpage搜索
     *
     * @internal
     */
    public function selectpage()
    {
        return parent::selectpage();
    }
}
